package com.scalar.chiptrack.rma.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.util.CellRangeAddress;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.rma.RMALineItem;
import com.scalar.chiptrack.rma.dao.RmaDAO;
import com.scalar.chiptrack.rma.forms.RMAForm;
import com.scalar.chiptrack.rma.forms.SendRMAMailForm;
import com.scalar.chiptrack.sales.ContactPerson;
import com.scalar.chiptrack.sales.SalesOrder;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.utils.PDFGenerator;
import com.scalar.chiptrack.utils.StringUtils;



/**
 * Author: Prabhanjan
 * Date: Mar 1, 2004
 * Time: 3:18:55 PM
 */

public class SendRMAMailAction extends ChipTrackAction
{

    public SendRMAMailAction ()
    {

    }

    public ActionForward execute ( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
    {

        HashMap info = new HashMap ();
        request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
        HashMap errors = new HashMap ();
        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
        SendRMAMailForm sendRmaMailForm = ( SendRMAMailForm ) form;
        String rmaId = request.getParameter ( "rmaId" );
        request.setAttribute ( "rmaId", rmaId );
        String rmaNo = null;
        String email = null;
        String customerId = null;
        String customerName = null;
        ArrayList rmaContactPersons = null; //Added by Krishna Prasad on 17 May 2004
        RMAForm rmaForm = new RMAForm ();
//added by Sarada on 12th Nov 2003 to get SalesORderNo
        if ( rmaId != null )
        {
            RmaDAO.getRmaDetails ( rmaForm, rmaId );
            rmaNo = rmaForm.getRmaNumber ();
            email = rmaForm.getEmail ();
            customerId = Integer.toString ( rmaForm.getCustomerId () );
            customerName = rmaForm.getCustomerName ();
//System.out.println("email  in action   "+email +" customerPO "+customerPO);
        }

        // Added by Prabhanjan on 31 Jan 2004
        String mimeType = sendRmaMailForm.getAttachMIMEType ();
        // Modified Kumara Swamy (06/21/2004)
        if ( ( mimeType == null ) || ( mimeType.trim ().length () == 0 ) )
        {
            mimeType = "PDF";
            sendRmaMailForm.setAttachMIMEType ( "PDF" );
        }

        String subject = null;
        HttpSession session = request.getSession ();
        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        if ( user == null )
        {
            //System.out.println( "user  in action      "+user);
            session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, "/sendRmaMail.do?" + request.getQueryString () );
            return ( mapping.findForward ( "login" ) );
        }

        boolean success = false;
        String action = request.getParameter ( "action" );

        String from = user.getEmail ();

        if ( ( action != null ) && ( action.equalsIgnoreCase ( "save" ) ) )
        {
            try
            {
                String to = StringUtils.concatStrings ( sendRmaMailForm.getToUsers (), Mailer.EMAIL_SEPARATOR );
                String cc = StringUtils.concatStrings ( sendRmaMailForm.getCcUsers (), Mailer.EMAIL_SEPARATOR );
                String bcc = StringUtils.concatStrings ( sendRmaMailForm.getBccUsers (), Mailer.EMAIL_SEPARATOR );

                // Added by Prabhanjan (10 May 2004)
                to = Mailer.getNotifiableEmails ( to, false );
                cc = Mailer.getNotifiableEmails ( cc, false );
                bcc = Mailer.getNotifiableEmails ( bcc, false );
                if ( cc == null )
                {
                    sendRmaMailForm.setCcUsers ( null );
                }
                if ( bcc == null )
                {
                    sendRmaMailForm.setBccUsers ( null );
                }
                // End of additions by Prabhanjan (10 May 2004)

                subject = sendRmaMailForm.getSubject ();

                if ( subject == null )
                {
                    subject = " RMA Acknowledgement for : " + customerId;
                }
                sendRmaMailForm.setSubject ( subject );
                String message = sendRmaMailForm.getMessage ().replaceAll("\n", "<br>");
                String host = request.getHeader ( "HOST" );
                // Added by Prabhanjan on 31 Jan 2004
                String rmaAckFile = null;
                if( "PDF".equalsIgnoreCase( mimeType  ) )
                {
                    rmaAckFile = getRmaAckPDFFile( rmaId ) ;
                }
                if( "HTML".equalsIgnoreCase( mimeType  ) )
                {
                    rmaAckFile = getRmaAckHtmlFile ( host, rmaId );
                }
                if( "Excel".equalsIgnoreCase( mimeType  ) )  // Added by Gandhi on 17th June 2004
                {
                    rmaAckFile = createExcelFile( rmaId );
                }

                String[] mailAttachments = null;

                // Added by Prabhanjan (10 May 2004)
                String isConfirm = ( String ) session.getAttribute ( "isConfirm" );
                String sysAdminEmail = ConfigManager.getSysAdminEmail ();
                String companyEmailId = sysAdminEmail.substring ( sysAdminEmail.indexOf ( "@" ) + 1, sysAdminEmail.length () );
                if ( isConfirm == null )
                {
                    to = Mailer.getNotifiableEmails ( to, false );
                    cc = Mailer.getNotifiableEmails ( cc, false );
                    bcc = Mailer.getNotifiableEmails ( bcc, false );
                    InternetAddress[] toAddress = null;
                    InternetAddress[] ccAddress = null;
                    InternetAddress[] bccAddress = null;

                    String emailIds = "";
                    if ( to != null )
                    {
                        toAddress = InternetAddress.parse ( to );
                        for ( int i = 0; i < toAddress.length; i++ )
                        {
                            InternetAddress temp = toAddress[i];
                            String address = temp.getAddress ();
                           //modified by Varma on 03-jun-04 to delete the ; for one email
                            /* if ( address.indexOf ( companyEmailId ) == -1 )
                            {
                                emailIds = emailIds + temp.getAddress () + "; ";
                            } */
                            if ( address.indexOf ( companyEmailId ) == -1 )
                            {
                               if (emailIds == "")
                               {
                                emailIds = emailIds + temp.getAddress () ;
                               }
                                else
                               {
                                   emailIds = emailIds +",  "+ temp.getAddress () ;
                               }
                            }
                        }
                    }
                    if ( cc != null )
                    {
                        ccAddress = InternetAddress.parse ( cc );
                        for ( int i = 0; i < ccAddress.length; i++ )
                        {
                            InternetAddress temp = ccAddress[i];
                            String address = temp.getAddress ();
                            if ( address.indexOf ( companyEmailId ) == -1 )
                            {
                               // emailIds = emailIds + temp.getAddress () + ";  ";
                               if (emailIds == "")
                               {
                                    emailIds = emailIds + temp.getAddress () ;
                               }
                                else
                               {
                                   emailIds = emailIds +",  "+ temp.getAddress () ;
                               }
                            }
                        }
                    }
                    if ( bcc != null )
                    {
                        bccAddress = InternetAddress.parse ( bcc );
                        for ( int i = 0; i < bccAddress.length; i++ )
                        {
                            InternetAddress temp = bccAddress[i];
                            String address = temp.getAddress ();
                            if ( address.indexOf ( companyEmailId ) == -1 )
                            {
                                //emailIds = emailIds + temp.getAddress () + ";  ";
                               if (emailIds == "")
                               {
                                    emailIds = emailIds + temp.getAddress () ;
                               }
                                else
                               {
                                   emailIds = emailIds +",  "+ temp.getAddress () ;
                               }
                            }
                        }
                    }

                    if ( emailIds.trim ().length () > 0 )
                    {
                        errors.put ( "The following e-mails are going out of the company. Please confirm to send them.", null );
                       // info.put ( "Emails :", emailIds );   commented by Varma to delete the :
                        info.put ( "Emails ", emailIds );

                        session.setAttribute ( "isConfirm", "True" );
                        return mapping.findForward ( "success" );
                    }

                }
                // End of additions by Prabhanjan (10 May 2004)


                // Modified by Prabhanjan on 31 Jan 2004
                // mailAttachments = new String[] { salesAckHtmlFile };
                mailAttachments = new String[]{rmaAckFile};
                Mailer.send ( from, to, cc, bcc, subject, message, mailAttachments, Mailer.CONTENT_TYPE_HTML, false );
                //commented by Varma to delete , after sent
                //info.put ( "Email has been sent, successfully", null );
                info.put ( "Email has been sent successfully", null );
                session.removeAttribute ( "isConfirm" );
            }
            catch ( Exception se )
            {
                success = false;
                String errMessage = "Internal Server Error";
                // Commented by Prabhanjan (12 May 2004)
                /*
                String errMessage = se.getMessage ();
                if ( ( errMessage == null ) || ( errMessage.trim ().length () == 0 ) )
                {
                    String errMessage = "Internal Server Error";
                }
                */
                errors.put ( errMessage, "Could not send Email" );
                se.printStackTrace ();
            }//catch

            return ( mapping.findForward ( "sendRmaMailView" ) );
        }//if

        //starts Here Added by Krishna Prasad on 17 May 2004 ,
        else if ( "showRmaContactPersonDetails".equalsIgnoreCase ( action ) )
        {
            try
            {
                String value = ( String ) session.getAttribute ( "RmaShowAgain" );
                if ( "True".equals ( value ) )
                {
                     rmaContactPersons = ( ArrayList ) session.getAttribute ( "RmaContactPersons" );
                }
                else
                {
                    rmaContactPersons = CustomerDAO.getAllContactPersonsWithEmails( rmaForm.getCustomerId () );
                    int contactPersonsSize = rmaContactPersons.size ();
                   /* for ( int i = 0; i < contactPersonsSize; i++ )
                    {
                        ContactPerson cperson = ( ContactPerson ) rmaContactPersons.get ( i );
                    } */
                }
            }
            catch ( ScalarException e )
            {
                e.printStackTrace ();
            }
            session.setAttribute ( "RmaShowAgain", "False" );
            request.setAttribute ( "RmaContactPersons", rmaContactPersons );
            return ( mapping.findForward ( "showRmaContactPersons" ) );
        }
        else if ( "displayMailIds".equalsIgnoreCase ( action ) )
        {
            try
            {
                // Commented by Krishna Prasad on 4th April 2004
                // contactPersons = CustomerDAO.getAllContactPersonsWithEmails ( salesOrder.getCustomerId());
                // Added by Krishna Prasad on 4th April 2004 to get Customer related ,Sales Order Billing  ,Sales Order Shipping - Contact Person Emalids

                rmaContactPersons = CustomerDAO.getAllContactPersonsWithEmails ( rmaForm.getCustomerId () );

                for ( int i = 0; i < rmaContactPersons.size (); i++ )
                {
                    ContactPerson contactPerson = ( ContactPerson ) rmaContactPersons.get ( i );

                    if ( request.getParameter ( "selectedValue" + i ) != null )
                    {
                        contactPerson.setIsChecked ( true );
                    }
                }
                session.setAttribute ( "RmaContactPersons", rmaContactPersons );
                session.setAttribute ( "RmaShowAgain", "True" );
            }
            catch ( Exception se )
            {
                System.out.println ( "Exception while loading sales order acknowledgement emails.. " );
            }
            return ( mapping.findForward ( "success" ) );
        }

        //ends Here Added by Krishna Prasad on 17 May 2004
        try
        {
//added by Sarada on 27th Oct 2003
            subject = sendRmaMailForm.getSubject ();
            if ( subject == null )
            {
//Modified by Sarada on 2nd Feb  2004 to add customer Name to subject
                subject = customerName + " RMA Acknowledgement for  " + rmaNo; //modified  by sarada to show customer PO in subject
            }
            sendRmaMailForm.setSubject ( subject );
//modified  by sarada to get default email into "To" field
            String[] mailTo = new String[1]; //Modified by Krishna Prasad on  19 May 2004
            if(( email == null ) || ( email.equalsIgnoreCase("") )) //Modified by Krishna Prasad on  19 May 2004
            {
                mailTo[0] = "";
            }
            else
            {
                mailTo[0] = email ;
            }
            sendRmaMailForm.setToUsers ( mailTo );
        }

        catch ( Exception se )
        {
            System.out.println ( "Exception while loading sales order acknowledgement emails.. " );
// se.getRootCause().printStackTrace();
        }

        //Added by Krishna Prasad on 17th May 2004 to remove 'RmaShowAgain' object from session because we get exception,
        //actually this is used to display the previous selected contact persons emailds for particular sales order
        session.removeAttribute ( "RmaShowAgain" );

        session.removeAttribute ( "isConfirm" );
        request.setAttribute ( "sendRmaMailForm", sendRmaMailForm );
        return ( mapping.findForward ( "success" ) );
    }
    public static String getRmaAckHtmlFile ( String host, String rmaId ) throws ScalarException
    {
        String filePath = null;
        RMAForm rmaForm = new RMAForm ();
        if ( rmaId != null )
        {
            RmaDAO.getRmaDetails ( rmaForm, rmaId );
        }

        ArrayList list = RmaDAO.getRmaLineItemDetails ( rmaId );

        ArrayList presentList = null;
        if(!list.isEmpty()){
            presentList = list;
        }else{
            presentList = getRmaLineItems();
        }


        String psAsHtml = formatAsHTML ( host, rmaForm, presentList );

        if ( psAsHtml == null )
        {
            return filePath;
        }

        FileWriter fWriter = null;
        File psAttachment = null;

        try
        {
            // CREATE TEMPORARY ATTACHMENT FILE
            File tempDir = new File ( "chiptrac_temp_dir" ).getCanonicalFile ();
            tempDir.mkdir ();

            String salesAttachmentFileName = null;
            String rmaNo = rmaForm.getRmaNumber ();
            //System.out.println( "salesNo      "+salesNo);
            salesAttachmentFileName = "RMA_ACK#  " + rmaNo + ".html";
            psAttachment = new File ( tempDir.getCanonicalPath (), salesAttachmentFileName );
            psAttachment.deleteOnExit ();

            if ( ( psAttachment.exists () == true ) || ( psAttachment.createNewFile () == true ) )
            {
                fWriter = new FileWriter ( psAttachment, false );

                fWriter.write ( psAsHtml );

                fWriter.close ();
                fWriter = null;

                filePath = psAttachment.getAbsolutePath ();
            }
        }
        catch ( Exception e )
        {
            System.out.println ( "Exception while creating Sales Order Ack html file" );
            e.printStackTrace ();
        }
        finally
        {
            if ( fWriter != null )
            {
                try
                {
                    fWriter.close ();
                    fWriter = null;
                }
                catch ( IOException ignore )
                {
                }
            }
        }

        return filePath;
    }

    // Changed by Prabhanjan on 3 Feb 2004. Method signature modified.Removed unnecessary parameters.
    // public static String formatAsHTML (String host,SalesOrder salesOrder,ArrayList presentList, int salesOrderId) throws ScalarException
    public static String formatAsHTML ( String host, RMAForm rmaForm, ArrayList rmaLineItemsList ) throws ScalarException
    {
        StringBuffer buf = new StringBuffer();
		//Added by Sarada on 5th March 2004 to get client Address
		SalesOrder salesOrder = new SalesOrder();
		String clientaddressId =  UserDAO.getClientAddressId();
		String addressId = null;
		String clientname = null;

		int finalAddressId = 0;
		if(clientaddressId != null)
		{
			StringTokenizer st = new StringTokenizer(clientaddressId,"@");
			try
			{
				addressId = st.nextToken();
				clientname = st.nextToken();
			}catch(NoSuchElementException e)
			{
				 clientname  = "";
			}
		}
		if(addressId  != null)
		{
			finalAddressId = Integer.parseInt(addressId);
		}
		Address clientAddress= UserDAO.loadClientAddressDetails( null , finalAddressId );
		salesOrder.setClientAddressAsObject(clientAddress);
		String clientAddress1 = SalesOrder.getClientAddressAsString(clientAddress);
        salesOrder.setClientAddress(clientAddress1);
		salesOrder.setClientName(clientname);
        //additions ended here
        String cancelImgPath = "";
        if("CANCELLED".equalsIgnoreCase(rmaForm.getRmaStatus())){
            cancelImgPath = "http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/cancelled_icon.gif";
        }

        String end_user_Label = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.sales.endCustomer" );
        if( end_user_Label == null )
        {
             end_user_Label = "Error in End_Customer";
        }
        String mfgPartNumberLabel = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.mfgPartNumber" );
        if( mfgPartNumberLabel == null )
        {
            mfgPartNumberLabel = Labels.DEFAULT_MFG_PART_NUMBER_LABEL;
        }

        buf.append ( "<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE><META http-equiv=Content-Type content='text/html; charset=windows-1252'>" );
        buf.append ( "<LINK href='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/stylesheets/scalarsoft.css' type=text/css rel=stylesheet>" );
        buf.append ( "<style type='text/css'>" );
        buf.append ( "a { text-decoration : none}" );
        buf.append ( "</style>" );
        buf.append ( "</HEAD><BODY bgColor='#ffffff' leftMargin=0 topMargin=0 marginheight='0' marginwidth='0'>" );
        buf.append ( "<center><h2>RMA Acknowledgement</h2></center>" );
        //buf.append ( "<P STYLE='page-break-after: always'>" );
        // Root Table
        buf.append ( "<table width='100%' cellspacing='0' cellpadding='0' border=0 align=center background="+ cancelImgPath +" ><tr><td>" );
        buf.append ( "<table width='98%' cellspacing='0' cellpadding='0' border=0 align=center >" );
        buf.append ( "<tr>" );
        buf.append ( "<td width='34%' align='center'><img src='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/scalar_logo.jpg' height='72' width='180' border=0></td>" );
        buf.append ( "<td width='1%'><img src='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/spacer.gif'></td>" );

        buf.append ("<td width='30%' align='center' class='NArialL'><br>"+salesOrder.getClientName());
        buf.append ("<br>" + salesOrder.getClientAddressAsHTML() );
        buf.append ("<td width='34%' align=center><font class='NArialW'>&nbsp;</font></td>" );
        buf.append ("</tr>" );
        buf.append ("<tr><td colspan=5><img src='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/spacer.gif' height=5></td></tr>" );
        buf.append ("<tr><td colspan=5><img src='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/spacer.gif' height=5></td></tr>" );
        buf.append ("<tr><td colspan=5><img src='http://" + host + "/" + ConfigManager.getChipTracContext () + "/jsp/includes/images/spacer.gif' height=5></td></tr>" );
        buf.append ("</table>" );
        buf.append ("<br>" );
        buf.append ("<table  width='95%' border='0' bordercolor='#000000' cellspacing='0' cellpadding='0' align='center'>" );
        buf.append ("<tr><td colspan=5 class='NArialL'><b><u>NOTE: RMA number is valid for only 30 days from issue date.</u></b></td></tr>");
        buf.append ("<tr><td colspan=5>&nbsp;</td></tr>");
        buf.append ("<tr><td width='30%'>" );
        buf.append ( "<table border=1 width='100%' height='140' bordercolor='#000000' cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#DEE1E2'>Ordered By :</td></tr>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#FFFFEE'>" );
        buf.append ( "<table width='100%' class='NArialL' height=80 cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr><td>" + rmaForm.getCustomerName () + "<br>" );
        buf.append ( rmaForm.getAddressAsHTML()== null ? "" :rmaForm.getAddressAsHTML()   );
		buf.append ( "</td></tr></table></td></tr></table></td>"  );
        buf.append ( "<td width='5%'>&nbsp;</td>" );
        buf.append ( "<td width='30%'>" );
        buf.append ( "<table border=1 width='100%' height='140' bordercolor='#000000' cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#DEE1E2'>Bill To :</td></tr>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#FFFFEE'>" );
        buf.append ( "<table width='100%' class='NArialL' height=80 cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr><td>" + rmaForm.getCustomerName () + "<br>" );
		buf.append ( (rmaForm.getAddressAsHTML() == null ? "" :rmaForm.getAddressAsHTML() ) + "</td></tr>"  );
		buf.append ( "</table></td></tr></table></td>" );
        buf.append ( "<td width='5%'>&nbsp;</td>" );
        buf.append ( "<td width='30%'> <table border=1 width='100%' height='140' bordercolor='#000000' cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#DEE1E2'>Ship To :</td></tr>" );
        buf.append ( "<tr height=20><td class='NArialL' bgcolor='#FFFFEE'>" );
        buf.append ( "<table border=0 width='100%' class='NArialL' height=80 cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr><td width='80%'>" + (rmaForm.getVendorAddressAsHTML() == null ? "" :rmaForm.getVendorAddressAsHTML()) + "</td></tr>"  );
		buf.append ( "</table></td></tr></table></td></tr>" );
        buf.append ( "<tr><td width='5%'>&nbsp;</td></tr>" );
        buf.append ( "<tr><td colspan=5>" );
        buf.append ( "<table border='1' width='100%' bordercolor='#000000' cellspacing='0' cellpadding='0'>" );
        buf.append ( "<tr height=20>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'>RMA Number</td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'>Contact Person</td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'> " + end_user_Label + " </td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'>Issue date</td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'>Requested Date</td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align='center'>Close Date</td>" );
        buf.append ( "</tr>" );
        buf.append ( "<tr height=20>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + rmaForm.getRmaNumber () + "&nbsp;</td>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + ( rmaForm.getContactPerson () == null ? "" : rmaForm.getContactPerson () ) + "&nbsp;</td>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + ( rmaForm.getEndUser () == null ? "" : rmaForm.getEndUser () ) + "&nbsp;</td>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + ( rmaForm.getRmaIssueDate () == null ? "" : rmaForm.getRmaIssueDate () )     + "&nbsp;</td>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + ( rmaForm.getRmaRequestDate () == null ? "" : rmaForm.getRmaRequestDate () ) + "&nbsp;</td>" );
        buf.append ( "<td class='NArialL' bgcolor='#FFFFEE' align='center'>" + ( rmaForm.getCloseDate () == null ? "" : rmaForm.getCloseDate () )           + "&nbsp;</td>" );
        buf.append ( "</tr></table></td></tr></table>" );
        buf.append ( "<br><br><br>" );
        buf.append ( "<tr><td colspan='5'>" );
        buf.append ( "<table width='95%' border=1 bordercolor='#000000' cellspacing='0' cellpadding='0' align='center'>" );
        buf.append ( "<tr height=20>" );
        buf.append ( "<td width='5%' class='NArialL' bgcolor='#DEE1E2' align=center>Line Item #</td>" );
        buf.append ( "<td width='5%' class='NArialL' bgcolor='#DEE1E2' align=center>PO Line Item #</td>" );
        buf.append ( "<td width='15%' class='NArialL' bgcolor='#DEE1E2' align=center>"+ mfgPartNumberLabel +"</td>" );
        buf.append ( "<td width='10%' class='NArialL' bgcolor='#DEE1E2' align=center>PO #</td>" );
        buf.append ( "<td width='10%' class='NArialL' bgcolor='#DEE1E2' align=center>SO #</td>" );
        buf.append ( "<td width='10%' class='NArialL' bgcolor='#DEE1E2' align=center>Invoice #</td>" );
        buf.append ( "<td width='10%' class='NArialL' bgcolor='#DEE1E2' align=center>Invoice Date</td>" );
        buf.append ( "<td width='10%' class='NArialL' bgcolor='#DEE1E2' align=center>Quantity</td>" );
        buf.append ( "<td width='25%' class='NArialL' bgcolor='#DEE1E2' align=center>Problem Description</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Price</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Extended Price</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Serial #</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Reason For Failure</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Exchange</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Credit</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>FA</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Receive Qty</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Remarks</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Close Qty</td>" );
        //buf.append ( "<td class='NArialL' bgcolor='#DEE1E2' align=center>Close Date</td>" );
        buf.append ( "</tr>" );

        RMALineItem rmaLineItem = null;

        //ArrayList rmaLineItemDetails = rmaForm.getLineItemList();
        ArrayList rmaLineItemDetails = rmaLineItemsList;
        int size= rmaLineItemDetails.size();

        for ( int i = 0; i < size; i++ )
        {
            rmaLineItem = ( RMALineItem ) rmaLineItemDetails.get ( i );
            buf.append ( "<tr height=20>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center>" + rmaLineItem.getLineItemNo () + "</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getPoLineItemNo () == 0 ? "" : Integer.toString(rmaLineItem.getPoLineItemNo()) ) + "&nbsp;</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center nowrap>" + ( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getPartNumber () ) + "&nbsp;</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getPoNumber() == null  ? "" : rmaLineItem.getPoNumber() ) + "&nbsp;</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getSalesOrderNumber() == null  ? "" : rmaLineItem.getSalesOrderNumber() ) + "&nbsp;</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center nowrap>" + (rmaLineItem.getInvoiceNumber () == null ? "" : rmaLineItem.getInvoiceNumber ()) + "&nbsp;</td>" );
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align=center wrap>" + (rmaLineItem.getInvoiceDate () == null ? "" : rmaLineItem.getInvoiceDate ()) + "&nbsp;</td>" );
            if( rmaLineItem.getPartNumber() == null )
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>&nbsp;</td>" );
            }
            else
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap align=center>"+ rmaLineItem.getQuantity() +"&nbsp;</td>" );
            }
            buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getProblemDescription () == null ? "" : rmaLineItem.getProblemDescriptionAsHTML () ) + "&nbsp;</td>" );
            //buf.append ( "<td bgcolor='#FFFFEE' width='7%' class='NArialL' align='right' wrap >"+ ( rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getUnitPriceAsString()+"" ) +"&nbsp;</td>" );
            //buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' align='right' wrap>"+ ( rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getExtendedPriceAsString()+"" ) +"&nbsp;</td>" );
            //buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getSerialNumber () == null ? "" : rmaLineItem.getSerialNumber () ) + "&nbsp;</td>" );
            //buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getReason () == null ? "" : rmaLineItem.getReason () ) + "&nbsp;</td>" );
            //buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getIsExchange() ? "Yes" : "" ) + "&nbsp;</td>" );
            //buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getIsCredit() ? "Yes" : "" ) + "&nbsp;</td>" );
            //buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getIsFa() ? "Yes" : "" ) + "&nbsp;</td>" ); // modified by Krishna Prasad on 20 May 2004 , just yes to Yes
            /*if(rmaLineItem.getPartNumber()==null)
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>&nbsp;</td>" );
            }
            else
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>"+ rmaLineItem.getReceiveQty() +"&nbsp;</td>" );
            }

            buf.append ( "<td  bgcolor='#FFFFEE'  class='NArialL' align=center>" + ( rmaLineItem.getRemarks () == null ? "" : rmaLineItem.getRemarksAsHTML () ) + "&nbsp;</td>" );

            if(rmaLineItem.getPartNumber()==null)
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>&nbsp;</td>" );
            }
            else
            {
                buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>"+ rmaLineItem.getCloseQty() +"&nbsp;</td>" );
            }
            buf.append ( "<td bgcolor='#FFFFEE'  class='NArialL' wrap>" + (rmaLineItem.getLineItemCloseDate () == null ? "" : rmaLineItem.getLineItemCloseDate ()) + "&nbsp;</td>" );*/
            buf.append ( "</tr>" );
        }
        
        buf.append ( "</table>" );
        //buf.append ( "</p>" );

        buf.append ( "<table  width='95%' cellspacing='5' cellpadding='0' border=0 align=center>" );

        buf.append("<tr><td class='NArialL'><u>Please read carefully:</u></td></tr>");
        buf.append("<tr><td class='NArialL'><li>Please write the RMA number on the outside of your package and include a copy of invoice and shipper to expedite processing time.</td></tr>");
        buf.append("<tr><td class='NArialL'><li>RMA department will refuse any shipments without a valid RMA number.</td></tr>");
        /*buf.append("<tr><td class='NArialL'><li>In order to obtain RMA number, fill out all the above information properly and fax this form to RMA department. We will contact you with your RMA number.</td></tr>");
        buf.append("<tr><td class='NArialL'><li>All warranties start from the original invoice date.</td></tr>");
        buf.append("<tr><td class='NArialL'><li>For more information on RMA policies please contact sales.</td></tr>");*/
        buf.append("<tr><td class='NArialL'>&nbsp;</td></tr>");

        /*
        buf.append ( "<TR>" );
        buf.append ( "<TD width='100%' valign='top' colspan='3' class='viewdata2'><center><b>Terms And Conditions</b></center></TD>" );
        buf.append ( "</TR>" );
        buf.append ( "<TR>" );
        //buf.append ( "<TD width='100%' valign='top' colspan='3' class='viewdata2'><center>" + ConfigManager.getClientAddressLine1 () + "<BR>" );
        //buf.append ( ConfigManager.getClientAddressLine2 () + "," + ConfigManager.getClientAddressLine3 () + "," + ConfigManager.getClientPhone () + "," + ConfigManager.getClientFax () + "</center></TD>" );
		buf.append ("<TD width='100%' valign='top' colspan='3' class='viewdata2'><center>"+salesOrder.getClientName()+"<BR>");
		buf.append (SalesOrder.getClientAddressToLoad(clientAddress)+"</center></TD>");
        buf.append ( "</TR>" );
        buf.append ( "<TR>" );
        buf.append ( "<TD width='100%' colspan='3' class='viewdata1'>&nbsp;</TD>" );
        buf.append ( "</TR>" );
        buf.append ( "<tr>" );

        ArrayList list = SalesDAO.loadTermsAndConditionsAsList ();
        */

        //(ArrayList)request.getAttribute("termsAndConditions");
        //System.out.println("list   "+list);
        /* if ( list != null )
        {
            for ( int i = 0; i < list.size (); i++ )
            {
                TermsAndConditions terms = ( TermsAndConditions ) list.get ( i );

                //buf.append ("<td width='49%' valign='top'><table width='100%' cellpadding='0' cellspacing='0' border='0' style='text-align:justify'>");
                buf.append ( "<tr><td bgcolor='#DEE1E2' class='viewdata1'>" + ( i + 1 ) + "." );
                //buf.append("+(i+1)+");
                buf.append ( "<U>" + terms.getTermsTitle ().trim () + "</U>" + terms.getTerms ().trim () + "</td></tr>" );
        */

            /*if(i == 0)
			{
                 buf.append ("<td width='49%' valign='top'><table width='100%' cellpadding='0' cellspacing='0' border='0' style='text-align:justify'>");
            }

			if(i == 9)
			{
				 buf.append ("</table></td><td valign='top' width='1%'>&nbsp;</td><td valign='top' width='50%'><table width='100%' cellpadding='0' cellspacing='0' border='0' style='text-align:justify'>");
            }

            */
                // Modified by Prabhanjan on 31 Jan 2004 to display terms and conditions starting from s.no. 1
                // buf.append ("<tr><td bgcolor='#DEE1E2' class='viewdata1'>"+i+".");
            /*if(i == list.size())
			{

                 buf.append ("</table></td>");
            }
            */
            //}//for

        //}//if

        buf.append ( "</tr></table>" );

        // Root table end
        buf.append ( "</td></tr></table>" );

        buf.append ( "</body></html>" );

        return buf.toString();
    }

    // Added by Prabhanjan on 11 May 2004 to get the PDF file
    public static String getRmaAckPDFFile ( String rmaId ) throws ScalarException
    {
        String fileXMLPath = null;
        String filePDFPath = null;
        String fileXSLTPath = null;

        RMAForm rmaForm = new RMAForm ();

        if ( rmaId != null )
        {
            RmaDAO.getRmaDetails ( rmaForm, rmaId );
        }

        ArrayList list = RmaDAO.getRmaLineItemDetails ( rmaId );

        ArrayList presentList = null;
        if( !list.isEmpty() )
        {
            presentList = list;
        }
        else
        {
            presentList = getRmaLineItems();
        }

        String saAsXML = formatAsXML ( rmaForm, presentList );

        if ( saAsXML == null )
        {
            return fileXMLPath;
        }

        FileWriter fWriter = null;
        File saPDFAttachment = null;
        File saXML = null;

        try
        {
            // CREATE TEMPORARY ATTACHMENT FILE
            String mailingTempDir = ConfigManager.getMailerTempDir ();
            File tempDir = new File ( mailingTempDir ).getCanonicalFile ();
            tempDir.mkdir ();

            String rmaNo = rmaForm.getRmaNumber ();
            String xmlFileName = "RMA_Ack_" + rmaNo + ".xml";
            String saAttachmentPDFFileName = "RMA_Ack_" + rmaNo + ".pdf";

            String xsltFileName = ConfigManager.getRmaAckXml2PdfXslFile ();

            saXML = new File ( tempDir.getCanonicalPath (), xmlFileName );
            saXML.deleteOnExit ();

            saPDFAttachment = new File ( tempDir.getCanonicalPath (), saAttachmentPDFFileName );
            saPDFAttachment.deleteOnExit ();

            if ( ( saXML.exists () == true ) || ( saXML.createNewFile () == true ) )
            {
                fWriter = new FileWriter ( saXML, false );
                fWriter.write ( saAsXML );
                fWriter.close ();
                fWriter = null;

                fileXMLPath = saXML.getCanonicalPath ().toLowerCase ();
                filePDFPath = saPDFAttachment.getCanonicalPath ().toLowerCase ();

                //System.out.println ( "XML Path " + fileXMLPath );
                //System.out.println ( "xslt Path " + xsltFileName );
                //System.out.println ( "PDF Path  " + filePDFPath );
                boolean success = PDFGenerator.convertXML2PDF ( fileXMLPath, xsltFileName, filePDFPath );

            }
        }
        catch ( Exception e )
        {
            System.out.println ( "Exception while creating Sales Order Ack PDF file" );
            e.printStackTrace ();
        }
        finally
        {
            if ( fWriter != null )
            {
                try
                {
                    fWriter.close ();
                    fWriter = null;
                }
                catch ( IOException ignore )
                {
                }
            }
        }

        return filePDFPath;
    }

    // Added by Prabhanjan on 11 May 2004 to generate XML file for PDF generation.
    public static String formatAsXML ( RMAForm rmaForm, ArrayList presentList ) throws ScalarException
    {
        StringBuffer buf = new StringBuffer ( 2098 );

        SalesOrder salesOrder = new SalesOrder();
        String clientaddressId =  UserDAO.getClientAddressId();
        String addressId = null;
        String clientname = null;

        int finalAddressId = 0;
        if(clientaddressId != null)
        {
            StringTokenizer st = new StringTokenizer(clientaddressId,"@");
            try
            {
                addressId = st.nextToken();
                clientname = st.nextToken();
            }catch(NoSuchElementException e)
            {
                 clientname  = "";
            }
        }
        if(addressId  != null)
        {
            finalAddressId = Integer.parseInt(addressId);
        }
        Address clientAddress= UserDAO.loadClientAddressDetails( null , finalAddressId );
        salesOrder.setClientAddressAsObject(clientAddress);
        String clientAddress1 = SalesOrder.getClientAddressAsString(clientAddress);
        salesOrder.setClientAddress(clientAddress1);
        salesOrder.setClientName(clientname);
        //Address clientAddress = salesOrder.getClientAddressAsObject ();

        String end_user_Label = MessageResources.getMessageResources( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage( "label.sales.endCustomer" );
        if ( end_user_Label == null )
        {
            end_user_Label = "Error in End_Customer";
        }

        String mfgPartNumberLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.mfgPartNumber" );
        if ( mfgPartNumberLabel == null )
        {
            mfgPartNumberLabel = Labels.DEFAULT_MFG_PART_NUMBER_LABEL;
        }

        buf.append ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" );
        buf.append ( "<rmaAcknowledgement>" + "\n" );
        //buf.append ( "<logo>d://chiptrack//jsp/includes/images/scalar_logo.jpg</logo>" + "\n" );
        buf.append ( "<logo>" + ConfigManager.getChipTracImagesDir () + ConfigManager.getScalarLogo () + "</logo>" + "\n" );
        buf.append ( "<selfAddress>" + "\n" );
        //Modified by Sarada on 5th March 2004 to get client Address
        buf.append ( "<name><![CDATA[" + salesOrder.getClientName () + "]]></name>" + "\n" );


        buf.append ( "<address1><![CDATA[" + clientAddress.getAddress1 () + "]]></address1>" + "\n" );
        buf.append ( "<address2><![CDATA[" + clientAddress.getAddress2 () + "]]></address2>" + "\n" );
        buf.append ( "<address3><![CDATA[" + clientAddress.getAddress3 () + "]]></address3>" + "\n" );
        buf.append ( "<address4><![CDATA[" + clientAddress.getAddress4 () + "]]></address4>" + "\n" );
        StringBuffer cityStateZip = new StringBuffer ( "" );

        if ( !clientAddress.getCity ().equals ( "" ) ) cityStateZip.append ( clientAddress.getCity () + ", " );
        if ( !clientAddress.getState ().equals ( "" ) ) cityStateZip.append ( clientAddress.getState () + " - " );
        if ( !clientAddress.getZipCode ().equals ( "" ) ) cityStateZip.append ( clientAddress.getZipCode () + ", " );
        cityStateZip.append ( clientAddress.getCountry () );

        buf.append ( "<cityStateZip><![CDATA[" + cityStateZip + "]]></cityStateZip>" + "\n" );

        StringBuffer phFax = new StringBuffer ();
        if ( !clientAddress.getPhoneNumber ().equals ( "" ) ) phFax.append ( "Ph." + clientAddress.getPhoneNumber () + ", " );
        if ( !clientAddress.getFaxNumber ().equals ( "" ) ) phFax.append ( "Fax." + clientAddress.getFaxNumber () );

        buf.append ( "<phoneFax><![CDATA[" + phFax + "]]></phoneFax>" + "\n" );


        buf.append ( "</selfAddress>" + "\n" );

        buf.append ( "<note><![CDATA[" + "RMA number is valid for only 30 days from issue date." + "]]></note>" + "\n" );
        buf.append ( "<information>" + "\n" );
        buf.append ( "<rmaNumber><![CDATA[" + ( rmaForm.getRmaNumber () == null ? "" : rmaForm.getRmaNumber () ) + "]]></rmaNumber>" + "\n" );
        buf.append ( "<contactPerson><![CDATA[" + ( rmaForm.getContactPerson () == null ? "" : rmaForm.getContactPerson () ) + "]]></contactPerson>" + "\n" );


        buf.append ( "<end_user_Label><![CDATA[" + end_user_Label + "]]></end_user_Label>" );
        buf.append ( "<endUser><![CDATA[" + ( rmaForm.getEndUser () == null ? "" : rmaForm.getEndUser () ) + "]]></endUser>" + "\n" );
        buf.append ( "<requestedDate><![CDATA[" + ( rmaForm.getRmaRequestDate () == null ? "" : rmaForm.getRmaRequestDate () ) + "]]></requestedDate>" + "\n" );
        buf.append ( "<issueDate><![CDATA[" + ( rmaForm.getRmaIssueDate () == null ? "" : rmaForm.getRmaIssueDate () ) + "]]></issueDate>" + "\n" );
        buf.append ( "<closeDate><![CDATA[" + ( rmaForm.getCloseDate () == null ? "" : rmaForm.getCloseDate () ) + "]]></closeDate>" + "\n" );
        buf.append ( "</information>" + "\n" );
        Address address = salesOrder.getBillingAddressAsObject ();

        buf.append ( "<orderByAddress>" + "\n" );

        buf.append ( "<name><![CDATA[" + rmaForm.getCustomerName () + "]]></name>" + "\n" );
        String contactPerson = "";
        if ( !( rmaForm.getCustomerName ().equals ( rmaForm.getContactPerson () ) ) )
        {
            contactPerson = rmaForm.getContactPerson ();
        }
        buf.append ( "<contactPerson><![CDATA[" + contactPerson + "]]></contactPerson>");
        buf.append ( "<address1><![CDATA[" + rmaForm.getAddress1 () + "]]></address1>" + "\n" );
        buf.append ( "<address2><![CDATA[" + rmaForm.getAddress2 () + "]]></address2>" + "\n" );
        buf.append ( "<address3><![CDATA[" + rmaForm.getAddress3 () + "]]></address3>" + "\n" );
        buf.append ( "<address4><![CDATA[" + rmaForm.getAddress4 () + "]]></address4>" + "\n" );
        buf.append ( "<city><![CDATA["+ rmaForm.getCity () + "]]></city>" + "\n" );

        buf.append ( "<stateZipCountry><![CDATA[" );
        if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
        {
            buf.append(rmaForm.getState ());
        }
        if(rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))
        {
            if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
            {
                buf.append(" - ");
            }
            buf.append(rmaForm.getZipcode());
        }
        if(rmaForm.getCountry ()!=null && !(rmaForm.getCountry ().equals("")))
        {
            if((rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))||(rmaForm.getState ()!=null && !(rmaForm.getState ().equals(""))))
            {
                buf.append(", ");
            }
            buf.append(rmaForm.getCountry ());
        }
        buf.append ( "]]></stateZipCountry>" + "\n" );
        buf.append ( "<phone><![CDATA[" + ( ( ( rmaForm.getPhoneNumber () != null ) && (rmaForm.getPhoneNumber() .trim() .length() > 0 ) )? "Tel:"+ rmaForm.getPhoneNumber () : "") + "]]></phone>" + "\n" );
        buf.append ( "<fax><![CDATA[" + ( ( ( rmaForm.getFaxNumber () != null ) && (rmaForm.getFaxNumber ().trim() .length() > 0) ) ? "Fax:" + rmaForm.getFaxNumber () : "") + "]]></fax>" + "\n" );
        buf.append ( "<email><![CDATA[" + ( ( ( rmaForm.getEmail () != null ) && (rmaForm.getEmail ().trim() .length() > 0 ) ) ? "Email:"+ rmaForm.getEmail () : "" ) + "]]></email>" + "\n" );
        buf.append ( "</orderByAddress>" + "\n" );

        buf.append ( "<billToAddress>" + "\n" );
        buf.append ( "<name><![CDATA[" + rmaForm.getCustomerName () + "]]></name>" + "\n" );
        buf.append ( "<contactPerson><![CDATA[" + contactPerson + "]]></contactPerson>");
        buf.append ( "<address1><![CDATA[" + rmaForm.getAddress1 () + "]]></address1>" + "\n" );
        buf.append ( "<address2><![CDATA[" + rmaForm.getAddress2 () + "]]></address2>" + "\n" );
        buf.append ( "<address3><![CDATA[" + rmaForm.getAddress3 () + "]]></address3>" + "\n" );
        buf.append ( "<address4><![CDATA[" + rmaForm.getAddress4 () + "]]></address4>" + "\n" );
        buf.append ( "<city><![CDATA["+ rmaForm.getCity () + "]]></city>" + "\n" );

        buf.append ( "<stateZipCountry><![CDATA[" );
        if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
        {
            buf.append(rmaForm.getState ());
        }
        if(rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))
        {
            if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
            {
                buf.append(" - ");
            }
            buf.append(rmaForm.getZipcode());
        }
        if(rmaForm.getCountry ()!=null && !(rmaForm.getCountry ().equals("")))
        {
            if((rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))||(rmaForm.getState ()!=null && !(rmaForm.getState ().equals(""))))
            {
                buf.append(", ");
            }
            buf.append(rmaForm.getCountry ());
        }
        buf.append ( "]]></stateZipCountry>" + "\n" );

        buf.append ( "<phone><![CDATA[" + ( ( ( rmaForm.getPhoneNumber () != null ) && (rmaForm.getPhoneNumber() .trim() .length() > 0 ) )? "Tel:"+ rmaForm.getPhoneNumber () : "") + "]]></phone>" + "\n" );
        buf.append ( "<fax><![CDATA[" + ( ( ( rmaForm.getFaxNumber () != null ) && (rmaForm.getFaxNumber ().trim() .length() > 0) ) ? "Fax:" + rmaForm.getFaxNumber () : "") + "]]></fax>" + "\n" );
        buf.append ( "<email><![CDATA[" + ( ( ( rmaForm.getEmail () != null ) && (rmaForm.getEmail ().trim() .length() > 0 ) ) ? "Email:"+ rmaForm.getEmail () : "" ) + "]]></email>" + "\n" );

        buf.append ( "</billToAddress>" + "\n" );

        buf.append ( "<shipToAddress>" + "\n" );
        buf.append ( "<vendorAddress><![CDATA[" + ( ( ( rmaForm.getVendorAddressString()!= null ) && (rmaForm.getVendorAddressString().trim() .length() > 0 ) ) ? rmaForm.getVendorAddressString() : "" ) + "]]></vendorAddress>" + "\n" );
        buf.append ( "</shipToAddress>" + "\n" );

        buf.append ( "<lineItems>" + "\n" );

        buf.append ( "<labelMfgPartNumber><![CDATA[" + mfgPartNumberLabel + "]]></labelMfgPartNumber>" );

        RMALineItem rmaLineItem = null;

        for ( int i = 0; i < presentList.size (); i++ )
        {
                    rmaLineItem = ( RMALineItem ) presentList.get ( i );

                    buf.append ( "<lineItem>" + "\n" );
                    buf.append ( "<lineItemNo><![CDATA[" + rmaLineItem.getLineItemNo () + "]]></lineItemNo>" + "\n" );
                    buf.append ( "<poLineItem><![CDATA[" + ( rmaLineItem.getPoLineItemNo () == 0 ? "" : Integer.toString(rmaLineItem.getPoLineItemNo()) ) + "]]></poLineItem>" + "\n" );
                    buf.append ( "<mfgPartNumber><![CDATA[" + ( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getPartNumber () ) + "]]></mfgPartNumber>" + "\n" );
                    buf.append ( "<poNo><![CDATA[" + ( rmaLineItem.getPoNumber() == null  ? "" : rmaLineItem.getPoNumber() ) + "]]></poNo>" + "\n" );
                    buf.append ( "<soNo><![CDATA[" + ( rmaLineItem.getSalesOrderNumber() == null  ? "" : rmaLineItem.getSalesOrderNumber() ) + "]]></soNo>" + "\n" );
                    buf.append ( "<invoiceNo><![CDATA[" + (rmaLineItem.getInvoiceNumber () == null ? "" : rmaLineItem.getInvoiceNumber ()) + "]]></invoiceNo>" + "\n" );
                    buf.append ( "<invoiceDate><![CDATA[" + (rmaLineItem.getInvoiceDate () == null ? "" : rmaLineItem.getInvoiceDate ()) + "]]></invoiceDate>" + "\n" );
                    buf.append ( "<qty><![CDATA[" + ( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getQuantity () + "" ) + "]]></qty>" + "\n" );
                    buf.append ( "<unitPrice><![CDATA[" + ( rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getUnitPriceAsString()+"" ) + "]]></unitPrice>" + "\n" );
                    buf.append ( "<extendedPrice><![CDATA[" + ( rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getExtendedPriceAsString()+"" ) + "]]></extendedPrice>" + "\n" );
                    buf.append ( "<problemDescription><![CDATA[" + ( rmaLineItem.getProblemDescription () == null ? "" : rmaLineItem.getProblemDescription () ) + "]]></problemDescription>" + "\n" );
                    buf.append ( "<exchange><![CDATA[" + ( rmaLineItem.getIsExchange() ? "Yes" : "" ) + "]]></exchange>" + "\n" );
                    buf.append ( "<credit><![CDATA[" + ( rmaLineItem.getIsCredit() ? "Yes" : "" ) + "]]></credit>" + "\n" );
                    buf.append ( "<fa><![CDATA[" + ( rmaLineItem.getIsFa() ? "Yes" : "" ) + "]]></fa>" + "\n" );
                    buf.append ( "<receiveQty><![CDATA[" + ( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getReceiveQty()  + "" ) + "]]></receiveQty>" + "\n" );
                    buf.append ( "<remarks><![CDATA[" + ( rmaLineItem.getRemarks () == null ? "" : rmaLineItem.getRemarks () ) + "]]></remarks>" + "\n" );
                    buf.append ( "<closeQty><![CDATA[" + ( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getCloseQty () + "" ) + "]]></closeQty>" + "\n" );
                    buf.append ( "<lineItemCloseDate><![CDATA[" + (rmaLineItem.getLineItemCloseDate () == null ? "" : rmaLineItem.getLineItemCloseDate ()) + "]]></lineItemCloseDate>" + "\n" );

                    buf.append ( "</lineItem>" + "\n" );
        }
        buf.append ( "</lineItems>" + "\n" );

        buf.append ( "<instructions>" + "\n" );
        buf.append ( "<instruction><description><![CDATA[" + "Please write the RMA number on the outside of your package and include a copy of invoice and shipper to expedite processing time." + "]]></description></instruction>" + "\n" );
        buf.append ( "<instruction><description><![CDATA[" + "RMA department will refuse any shipments without a valid RMA number." + "]]></description></instruction>" + "\n" );
        /*buf.append ( "<instruction><description><![CDATA[" + "In order to obtain RMA number, fill out all the above information properly and fax this form to RMA department. We will contact you with your RMA number." + "]]></description></instruction>" + "\n" );
        buf.append ( "<instruction><description><![CDATA[" + "All warranties start from the original invoice date." + "]]></description></instruction>" + "\n" );
        buf.append ( "<instruction><description><![CDATA[" + "For more information on RMA policies please contact sales." + "]]></description></instruction>" + "\n" );*/
        buf.append ( "</instructions>" + "\n" );

        buf.append ( "</rmaAcknowledgement>" + "\n" );

        return buf.toString ();
    }

    private static ArrayList getRmaLineItems(){

    	ArrayList list = new ArrayList();

    	for(int i=0; i<=6; i++){
    		RMALineItem lineItem = new RMALineItem();
            lineItem.setLineItemNo(i+1);
    		list.add(lineItem);
    	}
    	return list;
    }

  // Added by Gandhi on 23rd June 2004 for sending RMA Acknowledgement in Excel Format through Email
  public String createExcelFile(String rmaId) throws ScalarException
    {
          RMAForm rmaForm = new RMAForm ();
          if ( rmaId != null )
          {
              RmaDAO.getRmaDetails ( rmaForm, rmaId );
          }

          ArrayList list = RmaDAO.getRmaLineItemDetails ( rmaId );

          ArrayList presentList = null;
          if(!list.isEmpty()){
              presentList = list;
          }else{
              presentList = getRmaLineItems();
          }

          SalesOrder salesOrder = new SalesOrder();
          String clientaddressId =  UserDAO.getClientAddressId();
          String addressId = null;
          String clientname = null;

          int finalAddressId = 0;
          if(clientaddressId != null)
          {
              StringTokenizer st = new StringTokenizer(clientaddressId,"@");
              try
              {
                  addressId = st.nextToken();
                  clientname = st.nextToken();
              }catch(NoSuchElementException e)
              {
                   clientname  = "";
              }
          }
          if(addressId  != null)
          {
              finalAddressId = Integer.parseInt(addressId);
          }
          Address clientAddress= UserDAO.loadClientAddressDetails( null , finalAddressId );
          salesOrder.setClientAddressAsObject(clientAddress);
          String clientAddress1 = SalesOrder.getClientAddressAsString(clientAddress);
          salesOrder.setClientAddress(clientAddress1);
          salesOrder.setClientName(clientname);

            StringBuffer cityStateZip = new StringBuffer ( "" );
            if ( !(clientAddress.getCity ().equals ( "" )) && clientAddress.getCity () != null ) cityStateZip.append ( clientAddress.getCity ().trim() + ", " );
            if ( !(clientAddress.getState ().equals ( "" )) && clientAddress.getState () != null) cityStateZip.append ( clientAddress.getState ().trim() + " - " );
            if ( !(clientAddress.getZipCode ().equals ( "" )) && clientAddress.getZipCode () != null ) cityStateZip.append ( clientAddress.getZipCode ().trim() + ", " );
            cityStateZip.append ( clientAddress.getCountry () == null ? "":clientAddress.getCountry ().trim());

            StringBuffer phFax = new StringBuffer ();
            if ( !(clientAddress.getPhoneNumber ().equals ( "" )) && clientAddress.getPhoneNumber () != null ) phFax.append ( "Ph." + clientAddress.getPhoneNumber ().trim() + ", " );
            if ( !(clientAddress.getFaxNumber ().equals ( "" )) && clientAddress.getFaxNumber () != null) phFax.append ( "Fax." + clientAddress.getFaxNumber ().trim() );

            String contactPerson = "";
            StringBuffer orderBy = new StringBuffer();
            if ( !( rmaForm.getCustomerName ().equals ( rmaForm.getContactPerson () ) ) )
            {
                contactPerson = rmaForm.getContactPerson ();
            }
            if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
            {
                orderBy.append(rmaForm.getState ());
            }
            if(rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))
            {
                if(rmaForm.getState ()!=null && !(rmaForm.getState ().equals("")))
                {
                    orderBy.append(" - ");
                }
                orderBy.append(rmaForm.getZipcode());
            }
            if(rmaForm.getCountry ()!=null && !(rmaForm.getCountry ().equals("")))
            {
                if((rmaForm.getZipcode()!=null && !(rmaForm.getZipcode().equals("")))||(rmaForm.getState ()!=null && !(rmaForm.getState ().equals(""))))
                {
                    orderBy.append(", ");
                }
                orderBy.append(rmaForm.getCountry ());
            }


          Workbook wb = null;
           if ( "xlsx".equalsIgnoreCase(ConfigManager.getXLSWorkBookFormat() ) )
            {
                wb    = new XSSFWorkbook();
            }
            else
            {
                wb    = new HSSFWorkbook();
            }
            CreationHelper ch = wb.getCreationHelper();
            DataFormat df = ch.createDataFormat();
          Sheet sheet = wb.createSheet(rmaForm.getRmaNumber().trim());
          sheet.setDisplayGridlines(false);

          // Print Setup start here
          PrintSetup ps = sheet.getPrintSetup();
          sheet.setAutobreaks(true);
          sheet.setFitToPage(true);
          ps.setFitHeight((short)1);
          ps.setFitWidth((short)1);
          ps.setLandscape(true);
          // Print Setup end here

          CellStyle cs 		= null;
          CellStyle csBold 	= null;
          CellStyle csDataBold 	= null;
          CellStyle csUnderLine 	= null;
          CellStyle csBoldWrap 	= null;
          CellStyle csBoldWrapAlign 	= null;
          CellStyle csBoldWrapAlignRight 	= null;
          CellStyle csBoldAddress 	= null;
          CellStyle csBoldLineItems 	= null;
          CellStyle csBoldLineItemsAlignLeft 	= null;

          Font f 		= wb.createFont();
          Font fBold 	= wb.createFont();
          Font fDataBold 	= wb.createFont();
          Font fUnderLine	= wb.createFont();
          Font fBoldWrap 	= wb.createFont();
          Font fBoldWrapAlign	= wb.createFont();
          Font fBoldWrapAlignRight = wb.createFont();
          Font fBoldAddress = wb.createFont();
          Font fBoldLineItems = wb.createFont();
          Font fBoldLineItemsAlignLeft = wb.createFont();

          cs 				= wb.createCellStyle();
          csBold			= wb.createCellStyle();
          csDataBold      = wb.createCellStyle();
          csUnderLine     = wb.createCellStyle();
          csBoldWrap		= wb.createCellStyle();
          csBoldWrapAlign	= wb.createCellStyle();
          csBoldWrapAlignRight= wb.createCellStyle();
          csBoldAddress= wb.createCellStyle();
          csBoldLineItems= wb.createCellStyle();
          csBoldLineItemsAlignLeft= wb.createCellStyle();

          f.setFontName( "Arial" );
          f.setFontHeight( (short)200 );
          f.setBoldweight( Font.BOLDWEIGHT_BOLD );
          cs.setFont( f );
          cs.setWrapText(true);
          cs.setAlignment(CellStyle.ALIGN_CENTER);
          cs.setVerticalAlignment(CellStyle.VERTICAL_TOP);

          fBold.setFontName( "Arial" );
          fBold.setFontHeight( (short)320 );
          fBold.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBold.setFont( fBold );
          csBold.setAlignment(CellStyle.ALIGN_CENTER);

          fDataBold.setFontName( "Arial" );
          fDataBold.setFontHeight( (short)200 );
          fDataBold.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csDataBold.setFont( fDataBold );
          csDataBold.setWrapText(true);
          csDataBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          csDataBold.setAlignment(CellStyle.ALIGN_LEFT);
          csDataBold.setBorderBottom(CellStyle.BORDER_THIN);
          csDataBold.setBorderLeft(CellStyle.BORDER_THIN);
          csDataBold.setBorderRight(CellStyle.BORDER_THIN);
          csDataBold.setBorderTop(CellStyle.BORDER_THIN);

          fUnderLine.setFontName( "Arial" );
          fUnderLine.setFontHeight( (short)200 );
          fUnderLine.setBoldweight( Font.BOLDWEIGHT_BOLD );
          fUnderLine.setUnderline(Font.U_SINGLE);
          csUnderLine.setFont( fUnderLine );
          csUnderLine.setVerticalAlignment(CellStyle.VERTICAL_TOP);

          fBoldWrap.setFontName( "Arial" );
          fBoldWrap.setFontHeight( (short)200 );
          fBoldWrap.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldWrap.setFont( fBoldWrap );
          csBoldWrap.setWrapText(true);
          csBoldWrap.setVerticalAlignment(CellStyle.VERTICAL_TOP);

          fBoldWrapAlign.setFontName( "Arial" );
          fBoldWrapAlign.setFontHeight( (short)200 );
          fBoldWrapAlign.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldWrapAlign.setFont( fBoldWrapAlign );
          csBoldWrapAlign.setWrapText(true);
          csBoldWrapAlign.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          csBoldWrapAlign.setAlignment(CellStyle.ALIGN_CENTER);
          csBoldWrapAlign.setBorderBottom(CellStyle.BORDER_THIN);
          csBoldWrapAlign.setBorderLeft(CellStyle.BORDER_THIN);
          csBoldWrapAlign.setBorderRight(CellStyle.BORDER_THIN);
          csBoldWrapAlign.setBorderTop(CellStyle.BORDER_THIN);

          fBoldWrapAlignRight.setFontName( "Arial" );
          fBoldWrapAlignRight.setFontHeightInPoints( (short)9 );
          fBoldWrapAlignRight.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldWrapAlignRight.setFont( fBoldWrapAlignRight );
          csBoldWrapAlignRight.setWrapText(true);
          csBoldWrapAlignRight.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          csBoldWrapAlignRight.setAlignment(CellStyle.ALIGN_RIGHT);
          csBoldWrapAlignRight.setBorderBottom(CellStyle.BORDER_THIN);
          csBoldWrapAlignRight.setBorderLeft(CellStyle.BORDER_THIN);
          csBoldWrapAlignRight.setBorderRight(CellStyle.BORDER_THIN);
          csBoldWrapAlignRight.setBorderTop(CellStyle.BORDER_THIN);

          fBoldAddress.setFontName("Verdana" );
          fBoldAddress.setFontHeightInPoints( (short)8 );
          fBoldAddress.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldAddress.setFont( fBoldAddress );
          csBoldAddress.setWrapText(true);
          csBoldAddress.setVerticalAlignment(CellStyle.VERTICAL_TOP);
          csBoldAddress.setAlignment(CellStyle.ALIGN_LEFT);
          csBoldAddress.setBorderBottom(CellStyle.BORDER_THIN);
          csBoldAddress.setBorderLeft(CellStyle.BORDER_THIN);
          csBoldAddress.setBorderRight(CellStyle.BORDER_THIN);
          csBoldAddress.setBorderTop(CellStyle.BORDER_THIN);

          fBoldLineItems.setFontName( "Arial" );
          fBoldLineItems.setFontHeightInPoints( (short)9 );
          fBoldLineItems.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldLineItems.setFont( fBoldLineItems );
          csBoldLineItems.setWrapText(true);
          csBoldLineItems.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
          csBoldLineItems.setAlignment(CellStyle.ALIGN_CENTER);
          csBoldLineItems.setBorderBottom(CellStyle.BORDER_THIN);
          csBoldLineItems.setBorderLeft(CellStyle.BORDER_THIN);
          csBoldLineItems.setBorderRight(CellStyle.BORDER_THIN);
          csBoldLineItems.setBorderTop(CellStyle.BORDER_THIN);

          fBoldLineItemsAlignLeft.setFontName( "Arial" );
          fBoldLineItemsAlignLeft.setFontHeightInPoints( (short)9 );
          fBoldLineItemsAlignLeft.setBoldweight( Font.BOLDWEIGHT_BOLD );
          csBoldLineItemsAlignLeft.setFont( fBoldLineItemsAlignLeft );
          csBoldLineItemsAlignLeft.setWrapText(true);
          csBoldLineItemsAlignLeft.setVerticalAlignment(CellStyle.VERTICAL_TOP);
          csBoldLineItemsAlignLeft.setAlignment(CellStyle.ALIGN_LEFT);
          csBoldLineItemsAlignLeft.setBorderBottom(CellStyle.BORDER_THIN);
          csBoldLineItemsAlignLeft.setBorderLeft(CellStyle.BORDER_THIN);
          csBoldLineItemsAlignLeft.setBorderRight(CellStyle.BORDER_THIN);
          csBoldLineItemsAlignLeft.setBorderTop(CellStyle.BORDER_THIN);


          sheet.setColumnWidth(  0, (short) 3000 );
          sheet.setColumnWidth(  1, (short) 3000 );
          sheet.setColumnWidth(  2, (short) 5000 );
          sheet.setColumnWidth(  3, (short) 1500 );
          sheet.setColumnWidth(  5, (short) 3500 );
          sheet.setColumnWidth(  8, (short) 3000 );
          sheet.setColumnWidth(  9, (short) 3000 );
          sheet.setColumnWidth(  11,(short) 2500 );

          Row row = sheet.createRow(0);
          Cell cell = row.createCell(6);
          cell.setCellType(Cell.CELL_TYPE_STRING);
          cell.setCellValue(ch.createRichTextString("RMA Acknowledgement"));
          cell.setCellStyle(csBold);

       // Commented by Gandhi on 17th Aug 2004
          /* row = sheet.createRow((short) 2);
            cell = row.createCell((short) 6);
            cell.setCellValue(salesOrder.getClientName() == null ? "" : salesOrder.getClientName());
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 3);
            cell = row.createCell((short) 6);
            cell.setCellValue(clientAddress.getAddress1() == null ? "" : clientAddress.getAddress1());
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 4);
            cell = row.createCell((short) 6);
            cell.setCellValue(clientAddress.getAddress2() == null ? "" : clientAddress.getAddress2());
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 5);
            cell = row.createCell((short) 6);
            cell.setCellValue(clientAddress.getAddress3() == null ? "" : clientAddress.getAddress3());
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 6);
            cell = row.createCell((short) 6);
            cell.setCellValue(clientAddress.getAddress4() == null ? "" : clientAddress.getAddress4()+"");
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 6);
            cell = row.createCell((short) 6);
            cell.setCellValue(cityStateZip.toString());
            cell.setCellStyle(cs);
            row = sheet.createRow((short) 7);
            cell = row.createCell((short) 6);
            cell.setCellValue(phFax.toString());
            cell.setCellStyle(cs);  */
        // Ends here - Gandhi


        // Added by Gandhi on 17th Aug 2004
            String clientName = "";
            String clientAddr1 = "";
            String clientAddr2 = "";
            String clientAddr3 = "";
            String clientAddr4 = "";
            if( salesOrder.getClientName() != null )
            {
               if( salesOrder.getClientName().trim().length() != 0 )
               clientName = salesOrder.getClientName().trim()+"\n";
            }
            if( clientAddress.getAddress1 () != null )
            {
               if( clientAddress.getAddress1 ().trim().length() != 0 )
               clientAddr1 = clientAddress.getAddress1().trim()+"\n";
            }
            if( clientAddress.getAddress2 () != null )
            {
              if(clientAddress.getAddress2 ().trim().length() != 0 )
              clientAddr2 = clientAddress.getAddress2().trim()+"\n";
            }
            if( clientAddress.getAddress3 () != null )
            {
              if(clientAddress.getAddress3 ().trim().length() != 0 )
              clientAddr3 = clientAddress.getAddress3().trim()+"\n";
            }
            if( clientAddress.getAddress4 () != null )
            {
              if(clientAddress.getAddress4 ().trim().length() != 0 )
              clientAddr4 = clientAddress.getAddress4().trim()+"\n";
            }
            row = sheet.createRow((short) 2);
            cell = row.createCell( 4);
            cell.setCellValue(ch.createRichTextString(clientName+clientAddr1+clientAddr2+clientAddr3+clientAddr4+cityStateZip.toString().trim()+"\n"+phFax.toString().trim() ) );
            cell.setCellStyle(cs);

            //region = new Region( 2, (short) 4, 9, (short) 8 );
            sheet.addMergedRegion( new CellRangeAddress(2, 9, 4, 8) );
          // Ends here - Gandhi

          row = sheet.createRow((short) 11);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("NOTE: RMA number is valid for only 30 days from issue date." ));
          cell.setCellStyle(csUnderLine);

          row = sheet.createRow((short) 13);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("RMA Number :") );
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 1);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          //region = new Region( 13, (short) 0, 13, (short) 2 );
          sheet.addMergedRegion( new CellRangeAddress(13, 13, 0, 2 ));
          row = sheet.createRow((short) 14);
          cell = row.createCell( 0);
          cell.setCellValue( ch.createRichTextString(rmaForm.getRmaNumber().trim()));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 1);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          //region = new Region( 14, (short) 0, 14, (short) 2 );
          sheet.addMergedRegion( new CellRangeAddress(14, 14, 0, 2) );

          row  = sheet.createRow((short) 17);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("Contact Person :"));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 1);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);

          String end_user_Label = MessageResources.getMessageResources(Labels.APPLICATION_RESOURCE_PROPERTIES).getMessage("label.sales.endCustomer");
            if ( end_user_Label == null )
            {
             end_user_Label = "Error in End_Customer";
            }
          cell.setCellValue(ch.createRichTextString(end_user_Label+" :") );
          cell.setCellStyle(csDataBold);
          //region = new Region( 17, (short) 0, 17, (short) 1 );
          sheet.addMergedRegion( new CellRangeAddress( 17, 17, 0, 1) );
          row = sheet.createRow((short) 18);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString(rmaForm.getContactPerson () == null ? "" : rmaForm.getContactPerson () ) );
          cell.setCellStyle(csDataBold);
          cell = row.createCell(1);
          cell.setCellValue(ch.createRichTextString(""));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);
          cell.setCellValue(ch.createRichTextString( rmaForm.getEndUser () == null ? "" : rmaForm.getEndUser () ) );
          cell.setCellStyle(csDataBold);
          //region = new Region( 18, (short) 0, 18, (short) 1 );
          sheet.addMergedRegion( new CellRangeAddress( 18, 18, 0, 1) );

          row  = sheet.createRow((short) 21);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("Requested Date :"));
          cell.setCellStyle(csDataBold);
          cell = row.createCell(1);
          cell.setCellValue(ch.createRichTextString("Issue Date :"));
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);
          cell.setCellValue("Close Date :");
          cell.setCellStyle(csDataBold);
          row = sheet.createRow((short) 22);
          cell = row.createCell( 0);
          cell.setCellValue(rmaForm.getRmaRequestDate () == null ? "" : rmaForm.getRmaRequestDate ());
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 1);
          cell.setCellValue(rmaForm.getRmaIssueDate () == null ? "" : rmaForm.getRmaIssueDate ());
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 2);
          cell.setCellValue(rmaForm.getCloseDate () == null ? "" : rmaForm.getCloseDate ());
          cell.setCellStyle(csDataBold);

          row = sheet.createRow((short) 13);
          cell = row.createCell( 4);
          cell.setCellValue("Ordered By :");
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 5);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 6);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 7);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 9);
          cell.setCellValue("Bill To :");
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 10);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 11);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 12);
          cell.setCellStyle(csDataBold);


          String addr1 = "";
          String addr2 = "";
          String addr3 = "";
          String addr4 = "";
          String city = "";
          String stateCountry = "";
          String Tel = "";
          String Fax = "";
          String Email = "";

          if(contactPerson != null && !(contactPerson.equals("")))
             contactPerson = contactPerson+"\n";
          if(rmaForm.getAddress1 () != null && !(rmaForm.getAddress1 ().equals("")))
            addr1 = rmaForm.getAddress1 ()+"\n";
          if(rmaForm.getAddress2 () != null && !(rmaForm.getAddress2 ().equals("")))
            addr2 = rmaForm.getAddress2 ()+"\n";
          if(rmaForm.getAddress3 () != null && !(rmaForm.getAddress3 ().equals("")))
            addr3 = rmaForm.getAddress3 ()+"\n";
          if(rmaForm.getAddress4 () != null && !(rmaForm.getAddress4 ().equals("")))
            addr4 = rmaForm.getAddress4 ()+"\n";
          if(rmaForm.getCity () != null && !(rmaForm.getCity ().equals("")))
            city = rmaForm.getCity ()+"\n";
          if(!(orderBy.toString().equals("null")) && !(orderBy.toString().equals("")))
            stateCountry = orderBy.toString()+"\n";
          if(rmaForm.getPhoneNumber () == null )
              Tel = "Tel: ";
          else
              Tel = "Tel: "+rmaForm.getPhoneNumber ();
          if(rmaForm.getFaxNumber () == null )
              Fax = "Fax: ";
          else
              Fax = "Fax: "+rmaForm.getFaxNumber ();
          if(rmaForm.getEmail () == null )
              Email = "Email: ";
          else
              Email = "Email: "+rmaForm.getEmail ();

          row = sheet.createRow((short) 14);
          cell = row.createCell( 4);
          cell.setCellValue(rmaForm.getCustomerName()+"\n"+contactPerson+addr1+addr2+addr3+addr4+city+stateCountry+""+"\n"+Tel+"\n"+Fax+"\n"+Email);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          //row = sheet.createRow((short) 14);
          cell = row.createCell( 9);
          cell.setCellValue(rmaForm.getCustomerName()+"\n"+contactPerson+addr1+addr2+addr3+addr4+city+stateCountry+""+"\n"+Tel+"\n"+Fax+"\n"+Email);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 15);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 16);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 17);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 18);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 19);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 20);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 21);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csDataBold);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 22);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          row = sheet.createRow((short) 23);
          cell = row.createCell( 4);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 5);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 6);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 7);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 9);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 10);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 11);
          cell.setCellStyle(csBoldAddress);
          cell = row.createCell( 12);
          cell.setCellStyle(csBoldAddress);

          //region = new Region( 13, (short) 4, 13, (short) 7 );
          sheet.addMergedRegion( new CellRangeAddress( 13, 13, 4, 7) );
          //region = new Region( 13, (short) 9, 13, (short) 12 );
          sheet.addMergedRegion( new CellRangeAddress( 13, 13, 9, 12) );
          //region = new Region( 14, (short) 4, 23, (short) 7 );
          sheet.addMergedRegion( new CellRangeAddress( 14, 23, 4, 7) );
          //region = new Region( 14, (short) 9, 23, (short) 12 );
          sheet.addMergedRegion( new CellRangeAddress( 14, 23, 9, 12 ) );

          row = sheet.createRow((short) 30);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("Line Item #"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 1);
          cell.setCellValue(ch.createRichTextString("PO Line Item #"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 2);
          cell.setCellValue(ch.createRichTextString("Mfg Part Number"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 3);
          cell.setCellValue(ch.createRichTextString("PO #") );
          cell.setCellStyle(csBoldWrapAlign);

          cell = row.createCell( 4);
          cell.setCellValue(ch.createRichTextString("SO #"));
          cell.setCellStyle(csBoldWrapAlign);

        cell = row.createCell( 5);
          cell.setCellValue(ch.createRichTextString("Invoice #"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 6);
          cell.setCellValue(ch.createRichTextString("Invoice Date"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 7);
          cell.setCellValue(ch.createRichTextString("Quantity"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 8);
          cell.setCellValue(ch.createRichTextString("Price"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 9);
          cell.setCellValue(ch.createRichTextString("Extended Price"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 10);
          cell.setCellValue(ch.createRichTextString("Problem Desc"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 11);
          cell.setCellValue(ch.createRichTextString("Exchange"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 12);
          cell.setCellValue(ch.createRichTextString("Credit"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 13);
          cell.setCellValue(ch.createRichTextString("FA"));
          cell.setCellStyle(csBoldWrapAlign);
          cell = row.createCell( 14);
          cell.setCellValue(ch.createRichTextString("Receive Qty"));
          cell.setCellStyle(csBoldWrapAlign);

          cell = row.createCell( 15);
          cell.setCellValue(ch.createRichTextString("Remarks"));
          cell.setCellStyle(csBoldWrapAlign);

          cell = row.createCell( 16);
          cell.setCellValue(ch.createRichTextString("Close Qty"));
          cell.setCellStyle(csBoldWrapAlign);

          cell = row.createCell( 17);
          cell.setCellValue(ch.createRichTextString("Close Date"));
          cell.setCellStyle(csBoldWrapAlign);

              int counter = 0;
              RMALineItem rmaLineItem = null;
              for ( int i = 0; i < presentList.size (); i++ )
              {       counter = 31+i;
                      rmaLineItem = ( RMALineItem ) presentList.get ( i );

                      row = sheet.createRow((short) counter);

                      cell = row.createCell( 0);
                      cell.setCellValue( rmaLineItem.getLineItemNo () );
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 1);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getPoLineItemNo () == 0 ? "" : Integer.toString(rmaLineItem.getPoLineItemNo()) ) );
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 2);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getPartNumber () ) );
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 3);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getPoNumber() == null  ? "" : rmaLineItem.getPoNumber() ) );
                      cell.setCellStyle(csBoldLineItems);

                      cell = row.createCell( 4);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getSalesOrderNumber() == null  ? "" : rmaLineItem.getSalesOrderNumber() ) );
                      cell.setCellStyle(csBoldLineItems);

                      cell = row.createCell( 5);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getInvoiceNumber () == null ? "" : rmaLineItem.getInvoiceNumber () ));
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 6);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getInvoiceDate () == null ? "" : rmaLineItem.getInvoiceDate () ));
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 7);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getQuantity () + "" ) );
                      cell.setCellStyle(csBoldWrapAlignRight);
                      cell = row.createCell( 8);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getUnitPriceAsString()+"" ));
                      cell.setCellStyle(csBoldWrapAlignRight);
                      cell = row.createCell( 9);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getLineItemId() == -1 ? "" : rmaLineItem.getExtendedPriceAsString()+"" ));
                      cell.setCellStyle(csBoldWrapAlignRight);
                      cell = row.createCell( 10);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getProblemDescription () == null ? "" : rmaLineItem.getProblemDescription ().replaceAll("\r","") ));
                      cell.setCellStyle(csBoldLineItemsAlignLeft);
                      cell = row.createCell( 11);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getIsExchange() ? "Yes" : "" ) );
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 12);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getIsCredit() ? "Yes" : "" ) );
                      cell.setCellStyle(csBoldLineItems);
                      cell = row.createCell( 13);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getIsFa() ? "Yes" : "" ));
                      cell.setCellStyle(csBoldLineItems);

                      cell = row.createCell( 14);
                      cell.setCellValue( ch.createRichTextString( rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getQuantity () + "" ) );
                      cell.setCellStyle(csBoldWrapAlignRight);
                      cell = row.createCell( 15);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getRemarks () == null ? "" : rmaLineItem.getRemarks ().replaceAll("\r","")) );
                      cell.setCellStyle(csBoldLineItemsAlignLeft);
                      cell = row.createCell( 16);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getPartNumber () == null ? "" : rmaLineItem.getQuantity () + "" ) );
                      cell.setCellStyle(csBoldWrapAlignRight);
                      cell = row.createCell( 17);
                      cell.setCellValue( ch.createRichTextString(rmaLineItem.getLineItemCloseDate () == null ? "" : rmaLineItem.getLineItemCloseDate ()) );
                      cell.setCellStyle(csBoldLineItems);


              }

          row = sheet.createRow((short) counter+2);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("Please read carefully"));
          cell.setCellStyle(csUnderLine);
          row = sheet.createRow((short) counter+3);
          //row.setHeightInPoints((short) 28.5);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("* In order to obtain RMA number, fill out all the above information properly and fax this form to RMA department.We will contact you with your RMA number."));
          cell.setCellStyle(csBoldWrap);
          //region = new Region( counter+3, (short) 0, counter+3, (short) 14 );
          sheet.addMergedRegion( new CellRangeAddress(counter+3, counter+3, 0, 14) );
          row = sheet.createRow((short) counter+4);
          //row.setHeightInPoints((short) 28.5);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("* Please write the RMA number on the outside of your package and include a copy of invoice and shipper to expedite processing time."));
          cell.setCellStyle(csBoldWrap);
          //region = new Region( counter+4, (short) 0, counter+4, (short) 14 );
          sheet.addMergedRegion( new CellRangeAddress(counter+4, counter+4, 0, 14) );
          row = sheet.createRow((short) counter+5);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("* RMA department will refuse any shipments without a valid RMA number."));
          cell.setCellStyle(csBoldWrap);
          //region = new Region( counter+5, (short) 0, counter+5, (short) 14 );
          sheet.addMergedRegion( new CellRangeAddress(counter+5, counter+5, 0, 14 ) );
          row = sheet.createRow((short) counter+6);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("* All warranties start from the original invoice date."));
          cell.setCellStyle(csBoldWrap);
          //region = new Region( counter+6, (short) 0, counter+6, (short) 14 );
          sheet.addMergedRegion( new CellRangeAddress(counter+6, counter+6, 0, 14 ));
          row = sheet.createRow((short) counter+7);
          cell = row.createCell( 0);
          cell.setCellValue(ch.createRichTextString("* For more information on RMA policies please contact sales."));
          cell.setCellStyle(csBoldWrap);
          //region = new Region( counter+7, (short) 0, counter+7, (short) 14 );
          sheet.addMergedRegion( new CellRangeAddress(counter+7, counter+7, 0 ,14 ) );


        FileOutputStream fileOut;
        File file = null;
        String filePath = null;
        try
        {
              filePath = getServlet().getServletContext().getRealPath( ConfigManager.getRmaFilePath() );
              boolean exists = ( new File( filePath ) ).exists();
              if ( !exists )
              {
                  file = new File( filePath );
                  file.mkdirs();
              }
              String rmaNo = rmaForm.getRmaNumber ();
              fileOut = new FileOutputStream( filePath + "/"+"RMA_ACK#  " +rmaNo);
              wb.write( fileOut );
              fileOut.close();

              filePath = filePath + "/"+"RMA_ACK#  " +rmaNo;

        }
        catch ( Exception e)
        {
             e.printStackTrace();
        }

        return filePath;

     }

}